package vanillajdk8;

import java.awt.Shape;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.sound.midi.SysexMessage;

/**
 *
 * @author ymedina
 */
public class VanillaJDK8 {

    public static void main(String[] args) {
        List<String> rawList = Arrays.asList(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"});
        List<Integer> oddFilteredList = rawList.stream()
                .filter(s -> Integer.valueOf(s) % 2 != 0)
                .map(s -> Integer.valueOf(s))
                .collect(Collectors.toCollection((Supplier<List<Integer>>) ArrayList::new));

        oddFilteredList.stream().forEach(s -> System.out.println("Item: " + s));
        System.out.println("Reduce: " + oddFilteredList.stream().reduce(0, Integer::sum));

        arraysCreationOptionsJ8();
    }

    public static void arraysToListJ8() {
        Element[] array = {new Element(1), new Element(2), new Element(3)};
        ArrayList<Element> l2 = Stream.of(array).collect(Collectors.toCollection(ArrayList::new));
        l2.forEach(s -> System.out.println(s));

        List<Element> arrayList = Stream.of(array).collect(Collectors.toList());

//        Stream.of("one", "two", "three", "four")
//                .filter(e -> e.length() > 3)
//                .peek(e -> System.out.println("Filtered value: " + e))
//                .map(String::toUpperCase)
//                .peek(e -> System.out.println("Mapped value: " + e))
//                .collect(Collectors.toList());
    }

    public static void arraysCreationOptionsJ8() {
        List<String> listA = Stream.of("a", "B", "C").collect(Collectors.toList());
        List<Integer> listB = IntStream.range(10, 20).boxed().collect(Collectors.toList());
        List<Double> listC = DoubleStream.generate(() -> {
            return new Random().nextDouble();
        }).limit(10).boxed().collect(Collectors.toList());
        LinkedList<Integer> listD = Stream.iterate(0, x -> x++).limit(10).collect(Collectors.toCollection(LinkedList::new));

        listA.forEach(System.out::println);
        listB.forEach(System.out::println);
        listC.forEach(System.out::println);
        listD.forEach(System.out::println);
    }

    static class Element {

        private Integer id;

        public Element(Integer id) {
            this.id = id;
        }

        public Element() {
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "I'm Element :: " + this.id;
        }

    }

    class MinMax<E extends Comparable<E>> {

    }

    class A {
        void foo(int x) throws Exception {
            assert (x > 0);
            switch (x) {
                case 2: ;
                default: assert false;
            }
            throw new Exception();
        }
    }

    class SubA extends A {
        void foo() {
            System.out.println("B");
        }
    }

    class Tester {
        
    }

}
